package com.calc;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/*
 * Notes:
 * 	Need to figure out how to do color
 * 	And change text size
 */


@SuppressWarnings("deprecation")
public class CalculatorActivity extends Activity implements View.OnClickListener {
    private static final int BUTTON_WIDTH=120, 
    						 BUTTON_HEIGHT=110, 
    						 MARGIN=10,
    						 RESULT_FIELD_HEIGHT=140;
    private static Button[] NumberButtons, FunctionButtons;
    private static Button EqualsButton, DecimalButton;
    private static TextView result;
    
    private static double Value=0, tValue=0;
    private static boolean emptyTValue=false, emptyOperand=false;
    private static String operation="+";
    private static int decimalNumber=0;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        createButtons();
        setupMethods();
    }
    private void createButtons() {
        AbsoluteLayout layout = (AbsoluteLayout) findViewById(R.id.MainLayout);
        AbsoluteLayout.LayoutParams params;
        NumberButtons = new Button[10];
    //buttons 1-9
        for (int i=0 ;i<3; i++) {
        	for (int j=0 ;j<3; j++) {
        		params = new AbsoluteLayout.LayoutParams(BUTTON_WIDTH-MARGIN, BUTTON_HEIGHT-MARGIN, j*BUTTON_WIDTH, RESULT_FIELD_HEIGHT+(i+1)*BUTTON_HEIGHT);
        		NumberButtons[i*3+j+1]=new Button(this);
        		NumberButtons[i*3+j+1].setText(""+(i*3+j+1));
        		layout.addView(NumberButtons[i*3+j+1], params);
        	}
        }
    //0 button
        params = new AbsoluteLayout.LayoutParams(BUTTON_WIDTH*2-MARGIN, BUTTON_HEIGHT-MARGIN, 0, RESULT_FIELD_HEIGHT+4*BUTTON_HEIGHT);
		NumberButtons[0]=new Button(this);
		NumberButtons[0].setText("0");
		layout.addView(NumberButtons[0], params);
	//result field
		params = new AbsoluteLayout.LayoutParams(BUTTON_WIDTH*3, RESULT_FIELD_HEIGHT-MARGIN, 0, 0);
		result = new TextView(this);
		result.setText("NUMBERS GO HERE");
		result.setTextSize(24);
		//result.setBackgroundColor(990000);
		//result.setTextColor(0);
		layout.addView(result, params);
    //Function Buttons
		FunctionButtons = new Button[6];
		String functions = "C^/*-+";
		for (int i=0 ;i<4; i++) {
			params = new AbsoluteLayout.LayoutParams(BUTTON_WIDTH-MARGIN, BUTTON_HEIGHT-MARGIN, i*BUTTON_WIDTH, RESULT_FIELD_HEIGHT);
			FunctionButtons[i]=new Button(this);
			FunctionButtons[i].setText(functions.substring(i, i+1));
			layout.addView(FunctionButtons[i], params);
		}
        for (int i=0; i<2; i++) {
        	params = new AbsoluteLayout.LayoutParams(BUTTON_WIDTH-MARGIN, BUTTON_HEIGHT-MARGIN, 3*BUTTON_WIDTH, RESULT_FIELD_HEIGHT+(i+1)*BUTTON_HEIGHT);
        	FunctionButtons[4+i]=new Button(this);
        	FunctionButtons[4+i].setText(functions.substring(i+4, i+5));
        	layout.addView(FunctionButtons[i+4], params);
        }
    //Equals Button
        EqualsButton = new Button(this);
        params = new AbsoluteLayout.LayoutParams(BUTTON_WIDTH-MARGIN, 2*BUTTON_HEIGHT-MARGIN, 3*BUTTON_WIDTH, RESULT_FIELD_HEIGHT+3*BUTTON_HEIGHT);
        EqualsButton.setText("=");
        layout.addView(EqualsButton, params);
    //Decimal Button
        DecimalButton = new Button(this);
        params = new AbsoluteLayout.LayoutParams(BUTTON_WIDTH-MARGIN, BUTTON_HEIGHT-MARGIN, 2*BUTTON_WIDTH, RESULT_FIELD_HEIGHT+4*BUTTON_HEIGHT);
        DecimalButton.setText(".");
        layout.addView(DecimalButton, params);
        
        
        
        
        /*
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        Button[] NumberButtons = new Button[9];
        for (int i=0; i<3; i++) {
        	for (int j=0; j<3; j++) {
        		Button button = new Button(this);
        		NumberButtons[i*3+j]=button;
        		button.setText(""+(i*3+j+1));
        		params.addRule(RelativeLayout.ALIGN_PARENT_TOP, );
        		layout.addView(button, params);
        		//layout.addView(button, 120, 50);
        	}
        }*/
    }
    private void setupMethods() {
    	/*
    	EqualsButton.setOnClickListener(
    		new ViewOnClickListener() {
    			public void onClick(View v) {
    				calculate();
    			}
    	});
    	 */
        View.OnClickListener FunctionListener = new View.OnClickListener() {
        	public void onClick(View v) {
        		String tOperation;
        		Button tv = (Button) v;
        		tOperation=(String) tv.getText();
        		if (operation.equals("C")) {
        			emptyOperand=false;
        			operation="";
        		}
        		if (tOperation.equals("C")) {
        			operation=tOperation;
        			emptyOperand=false;
        		}
        		if (!emptyOperand) 
        			calculate();
        		emptyOperand=false;
        		operation=tOperation;
        	}
        };
        View.OnClickListener NumberListener = new View.OnClickListener() {
        	public void onClick(View v) {
        		Button tv = (Button) v;
        		String digitstr = (String) tv.getText();
        		int digit = Integer.parseInt(digitstr);
        		if (emptyTValue) {
        			emptyTValue=false;
        			tValue=0;
        		}
        		if (decimalNumber==0) {
        			tValue=10*tValue+digit;
        		} else {
        			tValue+=digit*Math.pow(10, -decimalNumber);
        			decimalNumber++;
        		}
        		display();
        	}
        };
        View.OnClickListener EqualsListener = new View.OnClickListener() {
        	public void onClick(View v) {
        		emptyOperand=true;
        		calculate();
        	}
        };
        View.OnClickListener DecimalListener = new View.OnClickListener() {
        	public void onClick(View v) {
        		if (decimalNumber==0)
        			decimalNumber=1;
        	}
        };
        
        
        
        
        EqualsButton.setOnClickListener(EqualsListener);
        DecimalButton.setOnClickListener(DecimalListener);
        for (Button b : FunctionButtons)
        	b.setOnClickListener(FunctionListener);
        for (Button b : NumberButtons)
        	b.setOnClickListener(NumberListener);
    }
    public void onClick(View v) {
    	
    }
    private void calculate() {
    	if (operation.equals("")) {
    		Value=tValue;
    		emptyTValue=true;
    	}
    	else if (operation.equals("C")) {
    		if (emptyTValue) {
    			Value=0;
    		} else {
    			tValue=0;
    			emptyTValue=true;
    		}
    	}
    	else if (operation.equals("^")) {
    		Value=Math.pow(Value, tValue);
    		emptyTValue=true;
    	}
    	else if (operation.equals("/")) {
    		Value=Value/tValue;
    		emptyTValue=true;
    	}
    	else if (operation.equals("*")) {
    		Value=Value*tValue;
    		emptyTValue=true;
    	}
    	else if (operation.equals("-")) {
    		Value=Value-tValue;
    		emptyTValue=true;
    	}
    	else if (operation.equals("+")) {
    		Value=Value+tValue;
    		emptyTValue=true;
    	}
    	decimalNumber=0;
    	display();
    }
    private void display() {
    	String text="";
    	if (!emptyTValue) {
    		text=""+tValue;
    	} else {
    		text=""+Value;
    	}
    	result.setText(text);
    }
}